<?php
class Admin_model extends CI_Model {
    function __construct() {
        parent::__construct();
        $this->load->database();  
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('session');
    }

    function checklogin($username, $password) // check xem admin de login
    {
        $sha1pass = sha1($password); 

        $query=$this->db->get("admin");
        //echo $sha1pass;


        foreach ($query->result() as $row)
        {
            //echo $row->NAME."</br>";
            //echo $username."</br>";
            //echo $row->PASSWORD."</br>";
            //echo $sha1pass;
            if($row->NAME == $username && $row->PASSWORD == $sha1pass)
            {
               return TRUE; 
            }
        }
        return FALSE;
    }   
    
    
    //Them khoa hoc moi
    function get_general_data() {
        $sql = "SELECT *
                FROM general_data";
        $query = $this->db->query($sql);
        foreach($query->result() as $row) {
            $data['course_quan'] = $row->course_quan_valiable;
            $data['course_id'] = $row->id_of_courses_valiable;
        }
        return $data;
     }
     
     function update_general_data($course_quan, $course_id) {
        $sql = "UPDATE general_data 
                SET course_quan_valiable = $course_quan, 
                    id_of_courses_valiable = '$course_id'";
        $this->db->query($sql);
     }
    
    //Tao bang diem moi
    function create_new_grade_table($table_name) {
        //$table_name = $this->convert_grade_table_name($course_code);
        $sql = "CREATE TABLE $table_name
                (ROW_ORDER INT(7) NOT NULL AUTO_INCREMENT,
                 USER_ROW_ORDER INT(7),
                 EMAIL_PUPIL VARCHAR(50),
                 GRADE VARCHAR(80) DEFAULT '00000000000000000000000000000000000000000000000000000000000000000000000000000000',
                 EMAIL_INCHARGE_TEACHER VARCHAR(101),
                 EMAIL_INCHARGE_EXCERCISE VARCHAR(50),
                 PRIMARY KEY (ROW_ORDER)
                )"; 
         $this->db->query($sql);            
    } 
    
    //Chon email va ma cac mon hoc chua dang ky cua bang nguoi dung
    function get_email_unregid_of_user(){
        $sql = "SELECT EMAIL, UNREGISTED_COURSE_QUAN, UNREGISTED_ID_OF_COURSES
                FROM user";
        $query = $this->db->query($sql);
        $i = 0;
        foreach ($query->result() as $row) {
            $data['user_email'][$i] = $row->EMAIL;
            $data['unreg_course_quan'][$i] = $row->UNREGISTED_COURSE_QUAN;
            $data['unreg_courses_id'][$i] = $row->UNREGISTED_ID_OF_COURSES;
            $i++;
        }
        return $data;
    }
    
    //cap nhat lai khoa hoc chua dang ky cua nguoi dung
    function update_unreg_courses_of_user($email, $unreg_course_quan, $unreg_courses_id) {
        $sql = "UPDATE user
                SET UNREGISTED_COURSE_QUAN = $unreg_course_quan,
                    UNREGISTED_ID_OF_COURSES = '$unreg_courses_id'
                WHERE EMAIL = '$email'";
        $this->db->query($sql);
    }
}
?>